<template>
  <div class="divWarehouseAdd">
    <div class="divHead">
      <el-form ref="form" :model="form" :rules="rules">
        <el-row style="width:100%">
          <el-col :span="8" class="el-col">
            <div>仓库编号</div>
            <el-form-item>
              <el-input v-model="form.code" :disabled="true" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="el-col">
            <div>仓库名称</div>
            <el-form-item prop="name">
              <el-input v-model="form.name" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="el-col">
            <div>仓库类型</div>
            <el-form-item prop="type">
              <el-select v-model="form.type" placeholder="请选择" style="width: 470px;">
                <el-option label="中转仓" value="ZZ" />
                <el-option label="加工仓" value="JG" />
                <el-option label="储备仓" value="CB" />
                <el-option label="冷藏仓" value="LC" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div>
          <div>省/市/区</div>
          <el-form-item prop="locId">
            <el-cascader
              v-model="locId"
              placeholder="请选择"
              style="width:960px"
              :options="options"
              @change="handleChange"
            />
          </el-form-item>
        </div>
        <el-row style="width:100%">

          <el-col :span="20" class="el-col">
            <div>详细地址</div>
            <el-form-item>
              <el-input v-model="form.address" placeholder="请输入内容" style="width: 960px" />
            </el-form-item>
          </el-col>
          <el-col :span="4" class="Single">
            <div>仓库状态</div>
            <el-radio v-model="form.status" :label="0">启用</el-radio>
            <el-radio v-model="form.status" :label="1">停用</el-radio>
          </el-col>
        </el-row>
        <el-row style="width:100%">
          <el-col :span="8" class="el-col">
            <div>仓库面积</div>
            <div style="margin-top: 15px;">
              <el-form-item>
                <el-input v-model="form.surface" placeholder="请输入内容">
                  <template slot="append">m²</template>
                </el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8" class="el-col">
            <div>负责人</div>
            <el-form-item>
              <el-input v-model="form.personName" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="8" class="el-col">
            <div>联系电话</div>
            <el-form-item prop="phone">
              <el-input v-model="form.phone" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="bottom-btn">
          <el-button round @click="$router.go(-1)">返回</el-button>
          <el-button type="warning" round @click="submit">提交</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addStorehouse, modifyStorehouse } from '@/api'
import { regionData, CodeToText } from 'element-china-area-data'

export default {
  data() {
    return {
      show: true,
      row: {},
      locId: [],
      newTableData: [],
      options: regionData, // 城市数据
      form: {
        phone: '', // 手机号
        personName: '', // 负责人姓名
        name: '', // 仓库名称
        location: '', // 地区全拼
        code: '', // 编码
        city: '', // 市id
        area: '', // 区id
        province: '', // 省id
        address: '', // 详细地址
        status: 0, // 状态
        surface: '', // 面积
        type: ''// 类型
      },
      rules: {
        name: [
          {
            required: true, message: '请输入仓库名称', trigger: 'blur'
          }
        ],
        phone: [
          {
            pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
          }
        ],
        type: {
          required: true, message: '请选择', trigger: 'blur'
        },
        locId: [
          {
            required: true, message: '请选择城市', trigger: 'blur'
          }
        ]

      }
    }
  },
  created() {
    if (this.$route.query.rows.code) {
      this.show = false
      console.log(this.$route.query.rows)
      const row = this.$route.query.rows
      this.row = row
      this.locId = row.area
      console.log(this.locId)
      console.log(row)
      // 回显
      this.form = {
        phone: row.phone, // 手机号
        personName: row.personName, // 负责人姓名
        name: row.name, // 仓库名称
        location: row.location, // 地区全拼
        code: row.code, // 编码
        city: row.city, // 市id
        area: row.area, // 区id
        province: row.province, // 省id
        address: row.address, // 详细地址
        status: row.status, // 状态
        surface: row.surface, // 面积
        type: row.type// 类型
      }
    } else {
      this.show = true
      if (this.$route.query.tableData[0] instanceof Object) {
        console.log(this.$route.query.tableData)
        this.newTableData = this.$route.query.tableData
        sessionStorage.setItem('newData', JSON.stringify(this.$route.query.tableData))
      } else {
        this.newTableData = JSON.parse(sessionStorage.getItem('newData'))
      }
      const num = this.newTableData[this.newTableData.length - 1].code
      const res = num.split('')
      res.splice(0, 2)
      const a = res.join('')
      res[res.length - 1] = Number(a) + 1
      this.form.code = 'CK' + res.join('')
    }
  },
  methods: {
    async submit() {
      if (this.show) {
        await addStorehouse(this.form)
        this.$message('添加成功')
        this.$router.go(-1)
        console.log(this.form)
      } else {
        console.log({ ...this.row, ...this.form })
        await modifyStorehouse({ ...this.row, ...this.form })
        this.$message('修改成功')
        this.$router.go(-1)
      }
    },
    handleChange() {
      // 多级选择
      var loc = ''
      for (let i = 0; i < this.locId.length; i++) {
        if (i === 2) {
          loc += CodeToText[this.locId[i]]
        } else {
          loc += CodeToText[this.locId[i]] + '-'
        }
      }
      // this.loc = loc
      console.log(loc)
      this.form.location = loc
      console.log(this.locId)
      // console.log(this.form.location)
      this.form.province = this.locId[0]
      this.form.city = this.locId[1]
      this.form.area = this.locId[2]
      // console.log(this.form.city)
    }
  }
}
</script>

<style lang="scss" scoped>
.divWarehouseAdd{
    height: 509px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
    margin-top: 20px;
    margin-left: 29px;
    margin-right: 29px;
    padding-top: 31px;
    padding-bottom: 30px;
    padding: 20px;
}
.divHead{
    width: 100%;
    height: 127px;
    // background: #fff;
    border-radius: 12px;
    // box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
    padding: 30px;
    margin-bottom: 20px;
    // display: flex;
    // justify-content: space-around;
    .el-col{
      margin-right: 20px;
      width: 470px;
    }
    div {
      font-size: 12px;
      color: #887e7e;
      margin-bottom: 12px;
    }
}
.Single{
  float: right;
  margin-top: 20px;
}
.bottom-btn{
  text-align: center;
}
</style>
